package com.lonewalker.sparrow.controller;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.excel.EasyExcel;
import com.lonewalker.sparrow.domain.ApiResult;
import com.lonewalker.sparrow.entity.SysUser;
import com.lonewalker.sparrow.service.FileService;
import com.lonewalker.sparrow.vo.FileVo;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.InputStream;
import java.util.List;

import static jdk.nashorn.internal.runtime.regexp.joni.Config.log;

/**
 * @author Wang Yu
 * @version 4.0
 */
@Slf4j
@RestController
@RequestMapping("/file")
public class FileController {

    @Resource
    private FileService fileService;

    /**
     * 文件上传本地
     * @param file 文件数据
     * @return 文件信息
     */
    @PostMapping("/uploadLocal")
    public ApiResult<FileVo> uploadLocal(@RequestPart("file") MultipartFile file){
        return fileService.uploadLocal(file);
    }

    @PostMapping("/upload")
    @SneakyThrows
    public void upload(MultipartFile file) {
        InputStream inputStream = file.getInputStream();
        List<SysUser> userList = EasyExcel.read(inputStream).head(SysUser.class).sheet(0).doReadSync();
        if (CollUtil.isNotEmpty(userList)){
            for (SysUser user : userList) {
                log.info("用户信息:{}",user);
            }
        }
    }
}
